import { getCommandDetail, postCommandSupervise } from '@/api/manageEnd/propagandizeDirective/allDirective'

import { objValue } from '@/utils/uitls'
import $bus from '@/utils/bus'

import { getColumns, createDiectiveFactor } from './service'
export default {
  methods: {
    /* 获取详情数据 */
    async getCommandDetail(id) {
      const res = await getCommandDetail(id)
      if (res.data.status !== 0) return
      // console.log('getCommandDetail', res)
      const data = res.data.data || {}
      this.directiveInfo = Object.freeze(data)

      /* 指令要素 
      // articleCount 只有稿件转发有 refreshCount 开机屏指令有
      // 前端无法确定是哪个指令，判断 值为 null 时，不显示
      */
      const factor = data.summary
      if (factor) {
        const source = createDiectiveFactor()
        source.forEach((item) => {
          item.forEach((it) => {
            it.value = objValue(factor, it.field)
          })
          const it = item.filter((it) => it.value !== null)
          if (it && it.length) {
            this.directiveFactor.push(it)
          }
        })
      }

      /* 指令概览 */
      const overview = data.overview
      if (overview) {
        const columns = overview.columns
        if (!columns) return

        const width = {
          title: 300,
          url: 300,
        }
        const pushColumns = getColumns(columns, width, 'overview')
        this.overview_columns.splice(1, 0, ...pushColumns)
        this.overview_data = Object.freeze(overview.list)
      }

      /* 网站执行情况统计 */
      const detail = data.detail
      if (detail) {
        const columns = detail.columns
        if (!columns) return
        /* 判断 未在指定位置 是否需要 */
        const hasLocation = detail.containLocation
        if (!hasLocation) {
          this.websiteExecute_columns[this.websiteExecute_columns.length - 1].children.splice(1, 1)
        }
        const pushColumns = getColumns(columns)
        this.websiteExecute_columns.splice(1, 0, ...pushColumns)
        this.websiteExecute_data = Object.freeze(detail.list)
      }

      /* 未执行情况 */
      const notExec = data.notExec
      if (notExec) {
        const columns = notExec.columns
        if (!columns) return
        this.unexecute_expand_columns = columns
        // notExec.list.forEach((it) => (it._expanded = false))
        this.unexecute_data = Object.freeze(notExec.list)
      }
    },

    /* 督办操作 */
    async postCommandSupervise(params, cb) {
      const res = await postCommandSupervise(params)
      const status = res.data.status
      if (status === 0) {
        this.$Message.success('督办成功')
        $bus.$emit('on-get-command-detail')
      } else {
        this.$Message.error(res.data.message || '督办失败，请稍后再试')
      }
    },
  },

  beforeDestroy() {
    $bus.$off()
  },
}
